package com.axend.aerosense.common.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.axend.aerosense.base.utils.DisplayUtils;
import com.axend.aerosense.common.R;

import java.lang.reflect.Field;

/**
 * Created by zzw on 2017/3/30.
 */

public abstract class CommonPopupWindow<B extends ViewDataBinding> {

    protected B binding;
    protected Context context;
    protected PopupWindow mInstance;
    private Window window;
    private int location;

    public CommonPopupWindow(Activity activity, int layoutId) {
        context = activity;
        window = activity.getWindow();
        this.binding = getBinding(layoutId);

        mInstance=new PopupWindow(binding.getRoot(),
                context.getResources().getDisplayMetrics().widthPixels,
                DisplayUtils.dip2px(100),
                true);

        initEvent(binding);
        initWindow();
    }

    private B getBinding(int layoutId){
        return DataBindingUtil.inflate(LayoutInflater.from(context), layoutId, null, false);
    }

    public View getContentView() {
        return binding.getRoot();
    }

    public PopupWindow getPopupWindow() {
        return mInstance;
    }

    protected abstract void initEvent(B binding);

    private void initWindow() {
        mInstance.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //在view外点击是否消失
        mInstance.setOutsideTouchable(false);
        mInstance.setTouchable(true);
        mInstance.setFocusable(false);
        fitPopupWindowOverStatusBar(mInstance, true);
        mInstance.setOnDismissListener(new android.widget.PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp=window.getAttributes();
                lp.alpha=1.0f;
                window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                window.setAttributes(lp);
            }
        });
    }

//    public void showBashOfAnchor(View anchor, LayoutGravity layoutGravity, int xmerge, int ymerge) {
//        int[] offset=layoutGravity.getOffset(anchor, mInstance);
//        mInstance.showAsDropDown(anchor, offset[0]+xmerge, offset[1]+ymerge);
//    }

    private void showAsDropDown(View anchor, int xoff, int yoff) {
        mInstance.showAsDropDown(anchor, xoff, yoff);
    }

    public void showAtLocation(View parent, int gravity){
        showAtLocation(parent, gravity, 0, 0);
    }

    //显示位置
    public void showAtLocation(View parent, int gravity, int x, int y) {
        this.location = gravity;
        if (location == Gravity.TOP){
            mInstance.setAnimationStyle(R.style.common_anim_popupwindow_top);
        }else if (location == Gravity.BOTTOM){
            mInstance.setAnimationStyle(R.style.common_anim_popupwindow_bottom);
        }
        mInstance.showAtLocation(parent, gravity, x, y);
        WindowManager.LayoutParams lp=window.getAttributes();
        //遮罩透明度
        lp.alpha=1.0f;
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        window.setAttributes(lp);
    }

    //隐藏窗口
    public void dismiss(){
        mInstance.dismiss();
    }

    public static void fitPopupWindowOverStatusBar(android.widget.PopupWindow mPopupWindow, boolean needFullScreen) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                Field mLayoutInScreen = android.widget.PopupWindow.class.getDeclaredField("mLayoutInScreen");
                mLayoutInScreen.setAccessible(needFullScreen);
                mLayoutInScreen.set(mPopupWindow, needFullScreen);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

//    public static class LayoutGravity {
//        private int layoutGravity;
//        // waring, don't change the order of these constants!
//        public static final int ALIGN_LEFT=0x1;
//        public static final int ALIGN_ABOVE=0x2;
//        public static final int ALIGN_RIGHT=0x4;
//        public static final int ALIGN_BOTTOM=0x8;
//        public static final int TO_LEFT=0x10;
//        public static final int TO_ABOVE=0x20;
//        public static final int TO_RIGHT=0x40;
//        public static final int TO_BOTTOM=0x80;
//        public static final int CENTER_HORI=0x100;
//        public static final int CENTER_VERT=0x200;
//
//        public LayoutGravity(int gravity) {
//            layoutGravity=gravity;
//        }
//
//        public int getLayoutGravity() { return layoutGravity; }
//        public void setLayoutGravity(int gravity) { layoutGravity=gravity; }
//
//        public void setHoriGravity(int gravity) {
//            layoutGravity&=(0x2+0x8+0x20+0x80+0x200);
//            layoutGravity|=gravity;
//        }
//        public void setVertGravity(int gravity) {
//            layoutGravity&=(0x1+0x4+0x10+0x40+0x100);
//            layoutGravity|=gravity;
//        }
//
//        public boolean isParamFit(int param) {
//            return (layoutGravity & param) > 0;
//        }
//
//        public int getHoriParam() {
//            for(int i=0x1; i<=0x100; i=i<<2)
//                if(isParamFit(i))
//                    return i;
//            return ALIGN_LEFT;
//        }
//
//        public int getVertParam() {
//            for(int i=0x2; i<=0x200; i=i<<2)
//                if(isParamFit(i))
//                    return i;
//            return TO_BOTTOM;
//        }
//
//        public int[] getOffset(View anchor, PopupWindow window) {
//            int anchWidth=anchor.getWidth();
//            int anchHeight=anchor.getHeight();
//
//            int winWidth=window.getWidth();
//            int winHeight=window.getHeight();
//            View view=window.getContentView();
//            if(winWidth<=0)
//                winWidth=view.getWidth();
//            if(winHeight<=0)
//                winHeight=view.getHeight();
//
//            int xoff=0;
//            int yoff=0;
//
//            switch (getHoriParam()) {
//                case ALIGN_LEFT:
//                    xoff=0; break;
//                case ALIGN_RIGHT:
//                    xoff=anchWidth-winWidth; break;
//                case TO_LEFT:
//                    xoff=-winWidth; break;
//                case TO_RIGHT:
//                    xoff=anchWidth; break;
//                case CENTER_HORI:
//                    xoff=(anchWidth-winWidth)/2; break;
//                default:break;
//            }
//            switch (getVertParam()) {
//                case ALIGN_ABOVE:
//                    yoff=-anchHeight; break;
//                case ALIGN_BOTTOM:
//                    yoff=-winHeight; break;
//                case TO_ABOVE:
//                    yoff=-anchHeight-winHeight; break;
//                case TO_BOTTOM:
//                    yoff=0; break;
//                case CENTER_VERT:
//                    yoff=(-winHeight-anchHeight)/2; break;
//                default:break;
//            }
//            return new int[]{ xoff, yoff };
//        }
//    }
}
